package com.onlyphoto.onlymall.mvp.view.binder

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.onlyphoto.onlymall.R
import com.bumptech.glide.Glide
import com.onlyphoto.onlymall.bean.response.GoodsBean
import com.onlyphoto.onlymall.mvp.view.activity.GoodsDetailsActivity

import me.drakeet.multitype.ItemViewBinder

/**
 * Created by gaochujia on 2019-07-09.
 */
class GoodsBinder : ItemViewBinder<GoodsBean, GoodsBinder.ViewHolder>() {

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.item_goods, parent, false)
        return ViewHolder(root)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, bean: GoodsBean) {
        viewHolder.update(bean)
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var img: ImageView? = null
        var title: TextView? = null
        var gift: TextView? = null
        var price: TextView? = null

        init {
            img = itemView.findViewById(R.id.img_goods)
            title = itemView.findViewById(R.id.title)
            price = itemView.findViewById(R.id.price)
            gift = itemView.findViewById(R.id.gift)
        }

        @SuppressLint("SetTextI18n")
        fun update(bean: GoodsBean) {
            Glide.with(img!!.context).load(bean.front_img).placeholder(R.mipmap.img_default).into(img!!)
            title?.text = bean.title
            if (bean.gift_set == 1) {
                gift?.visibility = View.VISIBLE
            } else {
                gift?.visibility = View.GONE
            }
            price?.text = "¥ " + bean.price_now.toString()
            itemView.setOnClickListener{
                itemView.context.startActivity(Intent(itemView.context, GoodsDetailsActivity::class.java)
                        .putExtra("goodsId", bean.id))
            }
        }
    }
}
